package com.usian.comment.service.impl;

import com.usian.comment.feign.UserFeign;
import com.usian.comment.service.ApCommentService;
import com.usian.model.comment.ApComment;
import com.usian.model.comment.ApCommentLike;
import com.usian.model.comment.ApCommentVo;
import com.usian.model.comment.dto.CommentDto;
import com.usian.model.comment.dto.CommentSaveDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApCommentServiceImpl implements ApCommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserFeign userFeign;


    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {

        if(dto==null ||dto.getArticleId()==null || StringUtils.isBlank(dto.getContent())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前用户及详情
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApUser apUser1 = userFeign.findById(apUser.getId());
        if(apUser1!=null){
            ApComment apComment = new ApComment();
            apComment.setAuthorId(apUser.getId());
            apComment.setAuthorName(apUser1.getName());
            apComment.setContent(dto.getContent());
            apComment.setEntryId(dto.getArticleId());
            apComment.setCreatedTime(new Date());
            apComment.setUpdatedTime(new Date());
            apComment.setImage(apUser1.getImage());
            apComment.setLikes(0);
            apComment.setReply(0);
            apComment.setType((short) 0);
            apComment.setFlag((short) 0);
            mongoTemplate.insert(apComment);
        }else{
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findByArticleId(CommentDto dto) {

        if(dto==null ||dto.getArticleId()==null || dto.getMinDate() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询所有评论  apcomment
        Query query = Query.query(Criteria.where("entryId").is(dto.getArticleId()).and("createdTime").lt(dto.getMinDate()));
        query.limit(5).with(Sort.by(Sort.Direction.DESC,"createdTime"));
        List<ApComment> apCommentList = mongoTemplate.find(query, ApComment.class);

        //查询 apcommentLike
        List<String> idList = apCommentList.stream().map(a -> a.getId()).collect(Collectors.toList());
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Query query1 = Query.query(Criteria.where("commentId").in(idList).and("authorId").is(apUser.getId()));
        List<ApCommentLike> commentLikeList = mongoTemplate.find(query1, ApCommentLike.class);


        //比较判断  得到有点赞数量的评论
        List<ApCommentVo> resultList = new ArrayList<>();
        if(commentLikeList.size()>0){
            //双层嵌套查询  得到 所有评论且标记评论的点赞标识
            apCommentList.stream().forEach(a->{
                ApCommentVo apCommentVo =new ApCommentVo();
                BeanUtils.copyProperties(a,apCommentVo);

                for(ApCommentLike apCommentLike :commentLikeList){
                    if(a.getId().equals(apCommentLike.getCommentId())){
                        apCommentVo.setOperation((short)0);
                        //查点赞数量
                        break;
                    }
                }
                resultList.add(apCommentVo);
            });
        }
        return ResponseResult.okResult(resultList);
    }
}
